#!/bin/bash

# IPS SERVERS
IP_WEB="192.168.25.1"
BROADCAST_WEB="192.168.25.255"

IP_TELNET1="10.61.5.130"
BROADCAST_TELNET1="10.61.5.255"
IP_TELNET2="10.61.6.129"
BROADCAST_TELNET2="10.61.5.255"

IP_FTP="10.111.25.1"
BROADCAST_FTP="10.111.25.127"

# DIRECCIONES DE RED
gianolla="192.168.25.0"
pauls="10.61.7.176"
aleandro="10.61.7.144"
gonzales="10.111.25.0"
chavez="10.111.25.128"
gimenez="10.61.7.180"
suar="10.61.6.128"
bertucelli="10.61.5.0"

# DIRECCIONES DE GATEWAYS
H2_gianolla="192.168.25.3"
H34_suar="10.61.6.131"
H13_gonzales="10.111.25.2"

# MASCARAS DE RED
Netmask24="255.255.255.0"
Netmask25="255.255.255.128"
Netmask26="255.255.255.192"
Netmask27="255.255.255.224"
Netmask28="255.255.255.240"
Netmask29="255.255.255.248"
Netmask30="255.255.255.252"


function web(){
	ifconfig $interfaz $IP_WEB broadcast $BROADCAST_WEB netmask $Netmask24 

	route add -net $pauls netmask $Netmask30 dev $interfaz metric 4 gw $H2_gianolla 
	route add -net $aleandro netmask $Netmask28 dev $interfaz metric 3 gw $H2_gianolla
	route add -net $gonzales netmask $Netmask25 dev $interfaz metric 3 gw $H2_gianolla
	route add -net $chavez netmask $Netmask25 dev $interfaz metric 2 gw $H2_gianolla
	route add -net $gimenez netmask $Netmask30 dev $interfaz metric 1 gw $H2_gianolla
	route add -net $suar netmask $Netmask27 dev $interfaz  metric 5 gw $H2_gianolla
	route add -net $bertucelli netmask $Netmask24 dev $interfaz metric 6 gw $H2_gianolla 

	cp grupo4.html /var/www/grupo4.html

	sudo /etc/init.d/apache2 restart

	./DNSresto.sh
}


function telnet(){
	#CONFIGURACION INTERFACES Y RUTA
	interfaz2=$interfaz":0"

	ifconfig $interfaz $IP_TELNET1 broadcast $BROADCAST_TELNET1 netmask $Netmask24 
	ifconfig $interfaz2 $IP_TELNET2 broadcast $BROADCAST_TELNET2 netmask $Netmask27

	route add -net $gianolla netmask $Netmask24 dev $interfaz metric 5 gw $H34_suar
 	route add -net $pauls netmask $Netmask30 dev $interfaz metric 1 gw $H34_suar
	route add -net $aleandro netmask $Netmask28 dev $interfaz metric 2 gw $H34_suar
	route add -net $gonzales netmask $Netmask25 dev $interfaz metric 3 gw $H34_suar
	route add -net $chavez netmask $Netmask25 dev $interfaz metric 3 gw $H34_suar
	route add -net $gimenez netmask $Netmask30 dev $interfaz metric 4 gw $H34_suar

	#CONFIGURACION SERVICIO
	cp inetd.conf.telnet /etc/inetd.conf
	/etc/init.d/openbsd-inetd restart 

	./DNSresto.sh
}

function ftp(){
	ifconfig $interfaz $IP_FTP broadcast $BROADCAST_FTP netmask $Netmask25

	route add -net $gianolla netmask $Netmask24 dev $interfaz metric 3 gw $H13_gonzales
	route add -net $aleandro netmask $Netmask28 dev $interfaz metric 1 gw $H13_gonzales
	route add -net $pauls netmask $Netmask30 dev $interfaz metric 2 gw $H13_gonzales 
	route add -net $chavez netmask $Netmask25 dev $interfaz metric 1 gw $H13_gonzales 
	route add -net $gimenez netmask $Netmask30 dev $interfaz metric 2 gw $H13_gonzales  
	route add -net $suar netmask $Netmask27 dev $interfaz metric 3 gw $H13_gonzales 
	route add -net $bertucelli netmask $Netmask24 dev $interfaz metric 4 gw $H13_gonzales 

	cp inetd.conf.ftp /etc/inetd.conf
	/etc/init.d/openbsd-inetd restart

	./DNStigre.sh
}

./limpiezaInterfaces.sh
interfaz=`ifconfig | egrep "eth" | sed 's/^\([A-Za-z0-9:]*\).*/\1/'`

#Desactivo el ruteo del sistema operativo
echo "0" >> /proc/sys/net/ipv4/ip_forward

case $1 in
web) web;;
telnet) telnet;;
ftp) ftp;;
*) echo "Error en los parámetros"
esac 
